package com.example.inerface;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import impl.NormalUserImpl;

public class NormalUserActivity extends Activity {
    String TAG="NarmnalUserActivity";
    private boolean mB;
    private NormalUserConnection mNormalUserConnection;

    private NormalUserAction1 mService1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_normal_user);
dobindService();


    }

    private void dobindService() {
        Intent intent = new Intent();
        intent.setAction("com.Service.ACTION_NOEMAL_USER");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setPackage(this.getPackageName());
        mNormalUserConnection = new NormalUserConnection();
        mB = bindService(intent, mNormalUserConnection, BIND_AUTO_CREATE);

    }



    public void saveMoneyClick(View view) throws RemoteException {
        Log.d(TAG,"saveMoneyClick");
        mService1.saveMoney(1000);
    }

    public void getMoneyClick(View view) throws RemoteException {
        Log.d(TAG,"getMoneyClick");
       float money=mService1.getMoney();
       Log.d(TAG,"getmaney-->"+money);
    }

    public void loanMoneyClick(View view) {
        Log.d(TAG,"loanMoneyClick");

    }



    private class NormalUserConnection implements ServiceConnection {




        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG,"onserviceConnection"+name);
            mService1 = NormalUserAction1.Stub.asInterface(service);

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG,"onserviceConnection"+name);

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mB&&mNormalUserConnection!=null)
        {
            Log.d(TAG,"unbind service");
unbindService(mNormalUserConnection);
mNormalUserConnection=null;
mB=false;
        }
    }
}
